
package com.yimixian.app.address;

import com.yimixian.app.R;
import com.yimixian.app.model.Address;
import com.yimixian.app.util.Strings;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class AddressViewForCart extends FrameLayout {

    @InjectView(R.id.name_text)
    TextView mNameText;
    @InjectView(R.id.address_text)
    TextView mAddressText;
    @InjectView(R.id.iv_edit)
    ImageView mIvEdit;
    @InjectView(R.id.rl_address_show)
    RelativeLayout mRlAddressShow;
    @InjectView(R.id.iv_no_address)
    ImageView mIvNoAddress;
    @InjectView(R.id.rl_address_no)
    RelativeLayout mRlAddressNo;

    public AddressViewForCart(Context context) {
        super(context);
        initAddressView();
    }

    public AddressViewForCart(Context context, AttributeSet attrs) {
        super(context, attrs);
        initAddressView();
    }

    public AddressViewForCart(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initAddressView();
    }

    private void initAddressView() {
        LayoutInflater.from(getContext()).inflate(R.layout.address_view_cart, this);
        ButterKnife.inject(this);
        setBackgroundColor(0xffffffff);
    }

    public void showNodata() {
        mRlAddressShow.setVisibility(View.GONE);
        mRlAddressNo.setVisibility(View.VISIBLE);
    }

    public void bind(Address address) {
        if (address == null) {
            return;
        }
        mRlAddressShow.setVisibility(View.VISIBLE);
        mRlAddressNo.setVisibility(View.GONE);
        StringBuilder nameBuilder = new StringBuilder(address.name);
        if (!Strings.isNullOrEmpty(address.gender)) {
            nameBuilder.append(" (" + address.gender + ")");
        }
        nameBuilder.append(Strings.repeat(" ", 4));
        if (!Strings.isNullOrEmpty(address.tel)) {
            nameBuilder.append(address.tel);
        }
        mNameText.setText(nameBuilder);

        boolean hasPoi = !Strings.isNullOrEmpty(address.poiId);
        //经纬度返回-1无poi信息，为手动填写地址
        if("-1".equals(address.latitude) && "-1".equals(address.longitude)){
            mAddressText.setText(address.poiAddress + " " + Strings.nullToEmpty(address.roomNo));
        }else{
            mAddressText.setText(hasPoi ? address.poiName + " " + Strings.nullToEmpty(address.roomNo)
                    : "[请补全当前地址]");
        }
        if("-1".equals(address.latitude) && "-1".equals(address.longitude)){
            mAddressText.setTextColor(0xff666666);
        }else{
            mAddressText
                    .setTextColor(hasPoi ? 0xff666666 : Color.RED);
        }

//        mAddressText.setText(hasPoi ? address.poiName + " " + address.roomNo : "[请补全当前地址]");
//        mAddressText.setTextColor(hasPoi ? 0xff666666 : Color.RED);
    }

    public void bind(String storeName, String tel) {
        mNameText.setText(tel);
        mAddressText.setText(storeName);
        mAddressText.setTextColor(0xff666666);
        mRlAddressShow.setVisibility(View.VISIBLE);
        mRlAddressNo.setVisibility(View.GONE);
    }

}
